<?php (defined('BASEPATH')) OR exit('No direct script access allowed');

class Groups extends Admin_Controller
{
	public function __construct()
	{
		parent::__construct();
		$this->module = "user";	
		$this->load->model('user_model');	
		$this->load->model('dx_auth/permissions');
	}
	
	public function index()
	{
		$this->crud->set_table('sys_roles');
		$this->crud->set_subject('Data groups');
		$this->crud->set_primary_key('id');
		$this->crud->required_fields('name');
		$this->crud->columns('name');
		$this->crud->display_as('name','Group Name');
		$this->crud->add_action('Permission', $this->icon->fa['key'], _URL.'user/groups/permission/');
		$this->crud->unset_read();
		$this->crud->unset_add_fields('id','parent_id');
		$this->crud->unset_edit_fields('id','parent_id');	
		
		$output = $this->crud->render();
		
		$data['output'] = $output;
		$data['page'] = "layout_groups";
		$data['module'] = $this->module;
		$this->load->view($this->layout_content,$data);
	}
	
	public function permission()
	{		
		$group_id = (int)$this->uri->segment(4);
		$arr_resources = $this->user_model->get_array_resources();
		
		$data['tree_resources'] = $this->user_model->get_tree_resources($arr_resources,$group_id);
		$data['module'] = $this->module;
		$data['page'] = 'layout_permission';
		$this->load->view($this->layout_content,$data);	
	}
	
	public function resources()
	{
		$resources = $this->user_model->get_array_resources();
		$this->output->set_content_type('application/json')->set_output(json_encode($resources));
	}
	
	public function save_permission()
	{
		$response = array();
		
		
		
		if( $this->input->post('group_id',true) )
		{
			if( $this->input->post('resources',true) )
			{
				$group_id = (int)$this->input->post('group_id',true);
				$resources = $this->user_model->get_all_resources();
				$access = $this->input->post('resources',true);
				
				if( count($access) > 0 )
				{
				
					$r_resources = array();
					foreach((array)$resources as $val)
					{
						$allow = ( in_array($val['id'],$access) ) ? true : false;
						$r_resources[$val['name']] = $allow;
					}
					
					$save = $this->permissions->set_permission_data($group_id,$r_resources);
					if($save)
					{
						$response['status'] = 1;
						$response['msg'] = "Data permission berhasil disimpan";
					}
					else
					{
						$response['status'] = 0;
						$response['msg'] = "Data permission gagal disimpan";	
					}
				}
				
			}
			else
			{
				$response['status'] = 0;
				$response['msg'] = "Silahkan pilih resources";	
			}
		}
		else
		{
			$response['status'] = 0;
			$response['msg'] = "Silahkan pilih resources";	
		}
		$this->output->set_content_type('application/json')->set_output(json_encode($response));
	}
	
	public function menu()
	{
		$resources = $this->__get_resources();
		$r_resources = $this->_resources($resources);
		
		$menu = $this->__get_menu();
		$r_menu = $this->_menu($menu);
		
		if( isset($_POST['add']) )
		{
			if( !empty($_POST['name']) && !empty($_POST['url']) )
			{
				$query = "INSERT INTO sys_menu SET 
								`name` = '".$_POST['name']."',
								parent_id = '".$_POST['menu_id']."',
								resources_id = '".$_POST['resources_id']."',
								url = '".$_POST['url']."',group_id = '".$_POST['group_id']."'";	
				$ok = $this->adodb->Execute($query);
				if($ok)
				{
					echo success("Disimpan");	
				}
			}
		}
		
		
		$data['menu'] = $r_menu;
		$data['resources'] = $r_resources;
		$data['module'] = $this->module;
		$data['page'] = 'layout_menu';
		$this->load->view($this->layout_content,$data);	
	}
	
	public function __get_resources( $parent_id = 0 )
	{
		$query = "SELECT * FROM sys_resources WHERE parent_id = $parent_id ORDER BY orderby DESC";
		$run = $this->adodb->Execute($query);
		
		$r = array();
		if( $run->RecordCount() > 0 )
		{
			while($row = $run->FetchRow())
			{
				$data['id'] = $row['id'];
				$data['name'] = $row['name'];
				$data['parent_id'] = $row['parent_id'];
				$data['child'] = $this->__get_resources($row['id']);
				$r[] = $data;
			}
		}
		return $r;	
	}
	
	public function _resources( $arr, $deep = 1 )
	{
		$out = "";
		foreach((array)$arr as $k)
		{
			$space = str_repeat('-',$deep);
			$out .= '<option value="'.$k['id'].'">'.$space.$k['name'].'</option>';	
			if( count($k['child']) > 0 )
			{
				$out .= $this->_resources($k['child'],($deep*2));	
			}			
		}
		return $out;
	}
	
	public function __get_menu( $parent_id = 0 )
	{
		$query = "SELECT * FROM sys_menu WHERE parent_id = $parent_id";
		$run = $this->adodb->Execute($query);
		
		$r = array();
		if( $run->RecordCount() > 0 )
		{
			while($row = $run->FetchRow())
			{
				$data['id'] = $row['id'];
				$data['name'] = $row['name'];
				$data['parent_id'] = $row['parent_id'];
				$data['child'] = $this->__get_menu($row['id']);
				$r[] = $data;
			}
		}
		return $r;	
	}
	
	public function _menu( $arr, $deep = 1 )
	{
		$out = "";
		foreach((array)$arr as $k)
		{
			$space = str_repeat('-',$deep);
			$out .= '<option value="'.$k['id'].'">'.$space.$k['name'].'</option>';	
			if( count($k['child']) > 0 )
			{
				$out .= $this->_resources($k['child'],($deep*2));	
			}			
		}
		return $out;
	}
}